home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 13.0 KB | 448 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Actions.h
- // Release Version: $ 1.0d11 $
- //
- // Author: Jim Lloyd
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef ACTIONS_H
- #include "Actions.h"
- #endif
-
- #ifndef BUTTONDEF_H
- #include "ButtonDef.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- #ifndef FWMEMMGR_H
- #include "FWMemMgr.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWFILESP_H
- #include "FWFileSp.h"
- #endif
-
- #ifndef FWRESOUR_H
- #include "FWResour.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef _ODTYPES_
- #include <ODTypes.h>
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODTranslation_xh
- #include <Translt.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODDragItemIterator_xh
- #include <DgItmIt.xh>
- #endif
-
- // ----- Macintosh Includes -----
-
- #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
- #include <Drag.h>
- #endif
-
- #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
- #include <Sound.h>
- #endif
-
- #ifndef __OSA__
- #include <OSA.h>
- #endif
-
- //========================================================================================
- // class CAction
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CAction::CAction
- //----------------------------------------------------------------------------------------
-
- CAction::CAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CAction::~CAction
- //----------------------------------------------------------------------------------------
-
- CAction::~CAction()
- {
- }
-
- //========================================================================================
- // class CSoundAction
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::CSoundAction
- //----------------------------------------------------------------------------------------
-
- CSoundAction::CSoundAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::~CSoundAction
- //----------------------------------------------------------------------------------------
-
- CSoundAction::~CSoundAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::IsInStorage
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CSoundAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean result = false;
- if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0)) // 'snd ' on Scrap
- result = true;
- else if (storage->Exists(ev, kODPropContents, kSoundFileKind, 0)) // sound file
- result = true;
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::Internalize
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::Internalize(Environment* ev,
- ODStorageUnit* storage)
- {
- FW_Boolean internalized = FALSE;
-
- if (storage->Exists(ev,
- kODPropContents, kSoundScrapKind, 0))
- {
- // Mac 'snd ' in Scrap
- storage->Focus(ev,
- kODPropContents,
- kODPosUndefined,
- kSoundScrapKind,
- 0,
- kODPosUndefined);
- InternalizeSound(ev, storage);
- }
- else if (storage->Exists(ev,
- kODPropContents, kSoundFileKind, 0))
- {
- // Mac sound file
- storage->Focus(ev,
- kODPropContents,
- kODPosUndefined,
- FW_CPart::gMachfsDataType,
- 0,
- kODPosUndefined);
- InternalizeSoundFile(ev, storage);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::InternalizeSound
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::InternalizeSound(Environment* ev,
- ODStorageUnit* storage)
- {
- // Assume the storage unit is already focused
- unsigned long size = storage->GetSize(ev);
- if (size > 0)
- {
- FW_CAcquireTemporarySystemHandle handle(size);
- FW_CByteArray byteArray;
- storage->GetValue(ev, size, byteArray);
- byteArray.CopyBuffer(handle.GetPointer(), size);
- fSoundHandle = handle.GetPlatformHandle();
- handle.Orphan();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::InternalizeSoundFile
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::InternalizeSoundFile(Environment* ev,
- ODStorageUnit* storage)
- {
- // StorageUnit should already be Focused on HFSFlavor
- unsigned long hfsSize = storage->GetSize(ev);
- if (hfsSize > 0)
- {
- // Get the HFS flavor
- HFSFlavor hfsInfo;
- FW_CByteArray byteArray;
- storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
- byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
-
- // Create platform independent file spec
- FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
-
- // Open the resource file
- FW_CResourceFile resourceFile(fileSpec);
-
- // Get any (i.e the first) sound resource
- const FW_ResourceType resourceType = 'snd ';
- const FW_ResourceId resourceId = 1;
- Handle sound = ::Get1IndResource(resourceType,
- resourceId);
-
- if (sound != NULL)
- {
- // if succeeded, detach the handle and keep it
- ::DetachResource(sound);
- fSoundHandle = sound;
- }
- else
- {
- // No sound resource loaded, throw an exception
- FW_FailOnError(::ResError());
- FW_Failure(resNotFound);
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::Externalize
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::Externalize(Environment* ev,
- ODStorageUnit* storage)
- {
- // Assume we're focused on the correct property
- if (!storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))
- storage->AddValue(ev, kSoundScrapKind);
-
- if (fSoundHandle != NULL)
- {
- FW_CAcquireLockedSystemHandle lock(fSoundHandle);
- FW_CByteArray byteArray(*fSoundHandle,
- FW_CMemoryManager::
- GetSystemHandleSize(fSoundHandle));
- storage->SetValue(ev, byteArray);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CSoundAction::DoIt
- //----------------------------------------------------------------------------------------
-
- void CSoundAction::DoIt()
- {
- if (fSoundHandle != NULL)
- {
- FW_CAcquireLockedSystemHandle lock(fSoundHandle);
- ::SndPlay(NULL, (SndListHandle)fSoundHandle, TRUE);
- }
- }
-
- //========================================================================================
- // class CScriptAction
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::CScriptAction
- //----------------------------------------------------------------------------------------
-
- CScriptAction::CScriptAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::~CScriptAction
- //----------------------------------------------------------------------------------------
-
- CScriptAction::~CScriptAction()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::IsInStorage
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CScriptAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean result = false;
- if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0)) // 'scpt' on Scrap
- result = true;
- else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0)) // compiled script file
- result = true;
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::Internalize
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::Internalize(Environment* ev, ODStorageUnit* storage)
- {
- FW_Boolean internalized = FALSE;
-
- if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0)) // 'scpt' on Scrap
- {
- storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
- InternalizeScript(ev, storage);
- }
- else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0)) // compiled script file
- {
- storage->Focus(ev, kODPropContents, kODPosUndefined, FW_CPart::gMachfsDataType, 0, kODPosUndefined);
- InternalizeScriptFile(ev, storage);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::InternalizeScript
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::InternalizeScript(Environment* ev, ODStorageUnit* storage)
- {
- // Assume the storage unit is already focused
- unsigned long size = storage->GetSize(ev);
- if (size > 0)
- {
- FW_CAcquireTemporarySystemHandle handle(size);
- FW_CByteArray byteArray;
- storage->GetValue(ev, size, byteArray);
- byteArray.CopyBuffer(handle.GetPointer(), size);
- fScriptHandle = handle.GetPlatformHandle();
- handle.Orphan();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::InternalizeScriptFile
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::InternalizeScriptFile(Environment* ev,
- ODStorageUnit* storage)
- {
- // StorageUnit should already be Focused on HFSFlavor
- unsigned long hfsSize = storage->GetSize(ev);
- if (hfsSize > 0)
- {
- // Get the HFS flavor
- HFSFlavor hfsInfo;
- FW_CByteArray byteArray;
- storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
- byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
-
- // Make the file specification
- FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
-
- // Acquire an opened resource file,
- // will be closed automatically
- FW_CResourceFile resourceFile(fileSpec);
-
- // Acquire an opened resource,
- // will be released automatically
- FW_CResource resource(resourceFile, 128, 'scpt');
- FW_CAcquireResourceData lockedResource(resource);
-
- // Copy the resource data into a new handle
- FW_CAcquireTemporarySystemHandle lockedHandle(lockedResource.GetSize());
- FW_CMemoryManager::CopyMemory(lockedResource.GetData(), lockedHandle.GetPointer(), lockedResource.GetSize());
- fScriptHandle = lockedHandle.GetPlatformHandle();
- lockedHandle.Orphan();
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::Externalize
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::Externalize(Environment* ev, ODStorageUnit* storage)
- {
- if (!storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))
- storage->AddValue(ev, kScriptScrapKind);
-
- if (fScriptHandle != NULL)
- {
- // Acquire a lock on the system handle, will be unlocked automatically
- FW_CAcquireLockedSystemHandle lock(fScriptHandle);
-
- // Copy the data into a SOM compatible object, and then write it to the storage unit
- FW_CByteArray byteArray(lock.GetPointer(), FW_CMemoryManager::GetSystemHandleSize(fScriptHandle));
- storage->SetValue(ev, byteArray);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // LoadAndExecuteScript
- //----------------------------------------------------------------------------------------
- // This function was lifted almost verbatim from
- // IM:IAC, page 10-16. (Actually, I used MPTA, but it says
- // the code is on the above page)
-
- void CScriptAction::LoadAndExecuteScript(Handle scriptData)
- {
- AEDesc scriptDesc;
- OSAID scriptID, resultID;
- OSAError error;
- static ComponentInstance gComponent = 0;
-
- if (!gComponent)
- gComponent = ::OpenDefaultComponent(
- kOSAComponentType,
- kOSAGenericScriptingComponentSubtype);
-
- // load the script data
- scriptDesc.descriptorType = typeOSAGenericStorage;
- scriptDesc.dataHandle = scriptData;
- error = ::OSALoad(gComponent, &scriptDesc,
- kOSAModeNull, &scriptID);
-
- if (error == noErr)
- {
- // execute the compiled script in the default context
- error = ::OSAExecute(gComponent,
- scriptID, kOSANullScript,
- kOSAModeNull, &resultID);
- error = ::OSADispose(gComponent, scriptID);
- error = ::OSADispose(gComponent, resultID);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CScriptAction::DoIt
- //----------------------------------------------------------------------------------------
-
- void CScriptAction::DoIt()
- {
- if (fScriptHandle != NULL)
- LoadAndExecuteScript(fScriptHandle);
- }
-